/*
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import olympicgameslogic.*;

/**
 *
 * @author Nuno
 */
public class JanelaPrincipal extends JFrame {

   public static JFrame jan;
   public static JDialog jd;
   private DataImporter di = new DataImporter();

   public JanelaPrincipal(String titulo) {
      super(titulo);



//carrega o utilizador activo em sistema.  

      StatusBar status = new StatusBar();
      add(status, java.awt.BorderLayout.SOUTH);
      status.setLeft(":: " + OlympicGamesGui.ap.getUtilizador().getUsername().toString() + "::");


      ClassLoader cldr = this.getClass().getClassLoader();
      super.setIconImage(Toolkit.getDefaultToolkit().getImage(cldr.getResource("images/extra/icon.png")));
      JLabel backgound = new JLabel(new ImageIcon(cldr.getResource("images/extra/LabelOL.jpg")));
      backgound.setPreferredSize(new Dimension(650, 450));
      backgound.setBackground(Color.red);
      backgound.setOpaque(false);
      add(backgound, BorderLayout.CENTER);



      JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar); //adiciona a barra de ferramentas à frame

      JMenu menu1, menu2, menu3, menu4, menu5, subMenu1, subMenu2, subMenu3, subMenu4;// = new JMenu();
      JMenuItem menuItem1, menuItem2, menuItem3, menuItem4, menuItem5, menuIteml;

      /**
       * ******************Menu FILE **********
       */
      menu1 = new JMenu("File");
      menu1.setMnemonic(KeyEvent.VK_F);
      menuBar.add(menu1);

//      subMenu1 = new JMenu("Options");      
//      menu1.add(subMenu1);

//      menuIteml = new JMenuItem("Language", KeyEvent.VK_L);
//      menuIteml.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
//      menuIteml.addActionListener(new ActionListener() {
//
//	 @Override
//	 public void actionPerformed(ActionEvent e) {
//            String[ ] opcoes = {"English", "Portuguese", "French"};             
//            
//            String livro = (String)JOptionPane.showInputDialog(JanelaPrincipal.this, "Select a Language", "Language Chooser", JOptionPane.PLAIN_MESSAGE, null, opcoes, opcoes[0]);                                     
//	    JOptionPane.showMessageDialog(JanelaPrincipal.this, livro.toString());	    
//
//	 }
//      });
//      menuIteml.setFocusable(false);

//      subMenu1.add(menuItem1);
//      menu1.addSeparator();

      menuItem1 = new JMenuItem("Logoff", KeyEvent.VK_O);
      menuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
      menuItem1.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    fechar();
	    new Login("Login");
	 }
      });
      menu1.add(menuItem1);
      menu1.addSeparator();

      menuItem1 = new JMenuItem("Exit", KeyEvent.VK_E);
      menuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));
      menuItem1.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    fechar();
	 }
      });
      menu1.add(menuItem1);


//      /**
//       * ****************** MENU Edit ******************
//       */
      menu2 = new JMenu("Edit");
      menu2.setMnemonic(KeyEvent.VK_D);
      menuBar.add(menu2);
//Submenu Editions

      menuItem2 = new JMenuItem("Editions");
      menuItem2.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    jan = new DialogEditionsMain("Editions Management");
	 }
      });
      menu2.add(menuItem2);


      //Submenu Delegations    

      menuItem2 = new JMenuItem("Delegations");
      menuItem2.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    jan = new DialogDelegMain("Delegations Management");


	 }
      });
      menu2.add(menuItem2);

//      menuItem2 = new JMenuItem("Manage");
//      menuItem2.addActionListener(new ActionListener() {
//
//	 @Override
//	 public void actionPerformed(ActionEvent e) {
//	    JOptionPane.showMessageDialog(JanelaPrincipal.this, e.getActionCommand());
//	 }
//      });
//      subMenu2.add(menuItem2);

      //Submenu Modalities
      menuItem2 = new JMenuItem("Sports");
      menu2.add(menuItem2);
      menuItem2.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {

	    jan = new DialogSportsMain("Sports Management");

	 }
      });


      //Submenu Import results

      menuItem2 = new JMenuItem("Import Edition Events");

      menuItem2.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JFileChooser fc = new JFileChooser();
	    int l = fc.showOpenDialog(JanelaPrincipal.this);
	    if (l == JFileChooser.APPROVE_OPTION) {
	       File f = fc.getSelectedFile();

	       try {
		  di.getEditionEventsData(OlympicGamesGui.ap, f);
	       } catch (InvalidFileException ex) {
		  JOptionPane.showMessageDialog(JanelaPrincipal.this, "Ficheiro: " + f.getName() + " Invalido");
	       } catch (FileNotFoundException ex) {
		  Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
	       } catch (IOException ex) {
		  Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
	       }




	    }
	 }
      });
      menu2.add(menuItem2);



      menuItem2 = new JMenuItem("Import Results");
      menuItem2.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JFileChooser fc = new JFileChooser();
	    int l = fc.showOpenDialog(JanelaPrincipal.this);
	    if (l == JFileChooser.APPROVE_OPTION) {
	       File f = fc.getSelectedFile();
	       try {
		  di.getEditionModalityResults(OlympicGamesGui.ap, f);
	       } catch (IOException ex) {
		  Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
	       } catch (InvalidFileException ex) {
		  JOptionPane.showMessageDialog(JanelaPrincipal.this, "Ficheiro: " + f.getName() + " Invalido");
	       }





	    }
	 }
      });
      menu2.add(menuItem2);




//      /////Menu de Consulta /////
      menu3 = new JMenu("Consult");
      menu3.setMnemonic(KeyEvent.VK_F);
      menuBar.add(menu3);

      subMenu3 = new JMenu("Ranking");
      menu3.add(subMenu3);

      menuItem3 = new JMenuItem("Nation");
      menuItem3.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (!OlympicGamesGui.ap.getEditions().isEmpty()) {
	       DialogRankingNations t = new DialogRankingNations(JanelaPrincipal.this, "Nation");
	       t.showJan();
	    }
	 }
      });
      subMenu3.add(menuItem3);

      menuItem3 = new JMenuItem("Athlete");
      menuItem3.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (!OlympicGamesGui.ap.getEditions().isEmpty()) {
	       DialogRankingAthlete t = new DialogRankingAthlete(JanelaPrincipal.this, "Athlete");
	       t.showJan();
	    }


	 }
      });
      subMenu3.add(menuItem3);

      menuItem3 = new JMenuItem("Modality");
      menuItem3.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (!OlympicGamesGui.ap.getEditions().isEmpty() && !OlympicGamesGui.ap.getModalities().isEmpty()) {
	       DialogRankingModality m = new DialogRankingModality(JanelaPrincipal.this, "Modality");
	       m.showJan();
	       //	    JOptionPane.showMessageDialog(JanelaPrincipal.this, e.getActionCommand());
	    }

	 }
      });
      subMenu3.add(menuItem3);

      menuItem3 = new JMenuItem("Discipline");
      menuItem3.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (!OlympicGamesGui.ap.getEditions().isEmpty() && !OlympicGamesGui.ap.getModalities().isEmpty()) {
	       DialogRankingDiscipline disc = new DialogRankingDiscipline(JanelaPrincipal.this, "Discipline");
	       disc.showJan();
	    }
	 }
      });
      subMenu3.add(menuItem3);

      menuItem3 = new JMenuItem("Comparision Analysis");
      menuItem3.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (!OlympicGamesGui.ap.getEditions().isEmpty()) {
	       DialogCompAnalysis t = new DialogCompAnalysis();
	       t.showJan();
	    }
	 }
      });
      menu3.add(menuItem3);


//      /////Menu de Gestão /////   
      menu4 = new JMenu("Management");
      menu4.setMnemonic(KeyEvent.VK_F);
      menuBar.add(menu4);

      menuItem4 = new JMenuItem("Application Users");
      menuItem4.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    jan = new DialogAppUsers("Appliaction User Management");
	 }
      });
      menu4.add(menuItem4);

      subMenu4 = new JMenu("Import Data");
      menu4.add(subMenu4);

      menuItem4 = new JMenuItem("IOC Codes");
      menuItem4.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JFileChooser fc = new JFileChooser();
	    int l = fc.showOpenDialog(JanelaPrincipal.this);
	    if (l == JFileChooser.APPROVE_OPTION) {
	       File f = fc.getSelectedFile();

	       try {
		  di.getNationData(OlympicGamesGui.ap, f);
	       } catch (FileNotFoundException ex) {
		  Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
	       } catch (IOException ex) {
		  Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
	       } catch (InvalidFileException ex) {
		  JOptionPane.showMessageDialog(JanelaPrincipal.this, "Ficheiro: " + f.getName() + " Invalido");
	       }

	    }
	 }
      });
      subMenu4.add(menuItem4);

      menuItem4 = new JMenuItem("IOC Sports");
      menuItem4.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JFileChooser fc = new JFileChooser();
	    int l = fc.showOpenDialog(JanelaPrincipal.this);
	    if (l == JFileChooser.APPROVE_OPTION) {
	       File f = fc.getSelectedFile();
	       try {

		  di.getSportsData(OlympicGamesGui.ap, f);
	       } catch (FileNotFoundException ex) {
		  Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
	       } catch (IOException ex) {
		  Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
	       } catch (InvalidFileException ex) {
		  JOptionPane.showMessageDialog(JanelaPrincipal.this, "Ficheiro: " + f.getName() + " Invalido");
	       }

	    }
	 }
      });
      subMenu4.add(menuItem4);

//      menuItem4 = new JMenuItem("Language");
//      menuItem4.addActionListener(new ActionListener() {
//
//	 @Override
//	 public void actionPerformed(ActionEvent e) {
//	    JOptionPane.showMessageDialog(JanelaPrincipal.this, e.getActionCommand());
//	 }
//      });
//      subMenu4.add(menuItem4);
//      
//      menuItem4 = new JMenuItem("Application Reset");
//      menuItem4.addActionListener(new ActionListener() {
//
//	 @Override
//	 public void actionPerformed(ActionEvent e) {
//	    JOptionPane.showMessageDialog(JanelaPrincipal.this, e.getActionCommand());
//	 }
//      });
//      menu4.add(menuItem4);



      //       // MENU HELP //
      menu5 = new JMenu("Help");
      menu5.setMnemonic('H');
      menuBar.add(menu5);

      menuItem5 = new JMenuItem("About", 'A');
      menuItem5.addActionListener(new ActionListener() {

	 public void actionPerformed(ActionEvent e) {
	    new DialogAbout(JanelaPrincipal.this, "About");
	 }
      });
      menu5.add(menuItem5);


      setDefaultCloseOperation(EXIT_ON_CLOSE);

      pack();
      setMinimumSize(new Dimension(getWidth(), getHeight()));

      setResizable(false);
      setLocationRelativeTo(null);
      setVisible(true);

      if (OlympicGamesGui.ap.getUtilizador().getPerfil() == AppUser.userType.admin) {
	 menu1.setEnabled(true);
	 menu2.setEnabled(true);
	 menu3.setEnabled(true);
	 menu4.setEnabled(true);
	 menu5.setEnabled(true);
      } else if (OlympicGamesGui.ap.getUtilizador().getPerfil() == AppUser.userType.edit) {
	 menu1.setEnabled(true);
	 menu2.setEnabled(true);
	 menu3.setEnabled(true);
	 menu4.setEnabled(false);
	 menu5.setEnabled(true);
      } else if (OlympicGamesGui.ap.getUtilizador().getPerfil() == AppUser.userType.consult) {
	 menu1.setEnabled(true);
	 menu2.setEnabled(false);
	 menu3.setEnabled(true);
	 menu4.setEnabled(false);
	 menu5.setEnabled(true);
      }

      addWindowListener(new WindowAdapter() {

	 @Override
	 public void windowClosing(WindowEvent e) {
	    fechar();
	 }
      });
   }

   public void fechar() {
      try {
	 DataExporter de = new DataExporter();
	 de.exportUsers(OlympicGamesGui.ap.getAppUsers());
	 OlympicGamesGui.saveData();
	 dispose();
      } catch (FileNotFoundException ex) {
	 Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IOException ex) {
	 Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
      }

   }
}
